<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 该页面功能完整
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="query.dictName"
          placeholder="名称"
          clearable
          class="handle-input mr10"
          @keyup.native.enter="handleSearch()"
        ></el-input>
        <el-input
          v-model="query.dictType"
          placeholder="类型"
          clearable
          class="handle-input mr10"
          @keyup.native.enter="handleSearch()"
        ></el-input>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button
          type="primary"
          @click="handleAdd"
          v-if="sdPermissions.includes('sys:dict:save')"
          >新增</el-button
        >
        <el-button
          type="danger"
          class="handle-del"
          @click="delAllSelection"
          v-if="sdPermissions.includes('sys:dict:delete')"
          >删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
        @sort-change="sortChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="dictName"
          label="名称"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="dictType"
          label="类型"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="sort"
          sortable
          label="排序"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="remark"
          label="备注"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="createDate"
          label="创建时间"
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.$index, scope.row)"
              v-if="sdPermissions.includes('sys:dict:update')"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="sdPermissions.includes('sys:dict:delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form
        ref="editForm"
        :model="editForm"
        label-width="auto"
        :rules="rules"
      >
        <el-form-item label="名称" prop="dictName">
          <el-input v-model="editForm.dictName" clearable></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="dictType">
          <el-input v-model="editForm.dictType" clearable></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="addForm.sort"
            controls-position="right"
            @change="handleChange"
            :min="1"
            :max="Infinity"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editForm.remark" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="50%">
      <el-form ref="addForm" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item label="名称" prop="dictName">
          <el-input
            v-model="addForm.dictName"
            placeholder="名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="dictType">
          <el-input
            v-model="addForm.dictType"
            placeholder="类型"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="addForm.sort"
            controls-position="right"
            @change="handleChange"
            :min="1"
            :max="Infinity"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="addForm.remark"
            placeholder="备注"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { sysDictMixin } from "common/js/systemSettings";
import util from "utils/util";
import { mapGetters } from "vuex";
export default {
  name: "sysdict",
  // mixins: [sysDictMixin],

  data() {
    return {
      sdPermissions: [],
      query: {
        order: "",
        orderField: "",
        pageIndex: 1,
        pageSize: 20,
        pid: "0",
        dictName: "",
        dictType: "",
        dictValue: "",
      },
      tableData: [
        {
          id: "",
          pid: "",
          dictType: "",
          dictName: "",
          dictValue: "",
          remark: "",
          sort: "",
          createDate: null,
          updateDate: null,
        },
      ],
      multipleSelection: [],
      rules: {
        dictName: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        dictType: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "必填不能为空", trigger: "blur" }],
      },
      addForm: {
        dictName: "",
        dictType: "",
        sort: 0,
        remark: "",
        pid: "0",
        dictValue: "",
      },
      editForm: {
        id: "",
        remark: "",
        createDate: "",
        dictName: "",
        dictType: "",
        sort: 0,
        pid: "0",
        dictValue: "",
        updateDate: "",
      },
      editVisible: false,
      addVisible: false,
      pageTotal: 0,

      idx: -1,
      ids: [],
    };
  },
  created() {
    this.getSDData({});
    this.sdPermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/sys-dict") {
        this.sdPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      // console.log(newVal, oldVal);
      this.sdPermissions = newVal;
    },
  },
  methods: {
    // 获取数据
    getSDData({
      order = this.query.order,
      orderField = this.query.orderField,
      page = this.query.pageIndex,
      limit = this.query.pageSize,
      pid = this.query.pid,
      dictName = this.query.dictName,
      dictType = this.query.dictType,
      dictValue = this.query.dictValue,
      _t = Date.now(),
    }) {
      this.$axios
        .get("/sys/dict/page", {
          params: {
            order,
            orderField,
            page,
            limit,
            pid,
            dictName,
            dictType,
            dictValue,
            _t,
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.pageTotal = res.data.total || 0;
          this.tableData = res.data.list;
        })
        .catch(() => {});
    },
    // 计数器
    handleChange(value) {
      this.addForm.sort = value;
      this.editForm.sort = value;
    },

    // 新增
    handleAdd() {
      this.addVisible = true;
    },
    saveAdd() {
      // 得到成功提示
      this.$axios
        .post("/sys/dict", {
          pid: this.addForm.pid,
          dictName: this.addForm.dictName,
          dictType: this.addForm.dictType,
          dictValue: this.addForm.dictValue,
          sort: this.addForm.sort,
          remark: this.addForm.remark,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.addVisible = false;
          this.getSDData({});
        })
        .catch(() => {});
    },

    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getSDData({
        order: this.query.order,
        orderField: this.query.orderField,
        dictName: this.query.dictName,
        dictType: this.query.dictType,
      });
    },
    // 表头排序
    sortChange({ column, prop, order }) {
      prop = prop === "createDate" ? "create_date" : prop;
      this.$set(this.query, "orderField", prop);
      if (order === "ascending") {
        this.$set(this.query, "order", order.substr(0, 3));
        this.getSDData({});
      } else if (order === "descending") {
        this.$set(this.query, "order", order.substr(0, 4));
        this.getSDData({});
      }
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids[0] = row.id;
      util.delete.call(this, "/sys/dict", index, this.ids, this.getSDData);
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      var ids = this.multipleSelection.map((item) => {
        return item.id;
      });
      util.delete.call(this, "/sys/dict", "", ids, this.getSDData);
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.editForm = row;
      this.editVisible = true;
      this.$axios
        .get("/sys/dict/" + row.id, {
          _t: Date.now(),
        })
        .then(({ data: res }) => {
          // console.log(res);
          if (res.code !== 0) {
            return;
          }
          this.editForm = res.data;
        })
        .catch(() => {});
    },
    // 保存编辑
    saveEdit() {
      // 得到成功提示
      this.$axios
        .put("/sys/dict", {
          id: this.editForm.id,
          pid: this.editForm.pid,
          dictName: this.editForm.dictName,
          dictType: this.editForm.dictType,
          dictValue: this.editForm.dictValue,
          sort: this.editForm.sort,
          remark: this.editForm.remark,
          createDate: this.editForm.createDate,
          updateDate: this.editForm.updateDate,
        })
        .then(({ data: res }) => {
          // console.log(res);
          if (res.code !== 0) {
            return;
          }
          this.editVisible = false;
          this.$message.success("操作成功");
          this.getSDData({});
        })
        .catch(() => {});
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getSDData({});
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getSDData({});
    },
  },

  // methods: {

  // }
};
</script>

<style scoped>
@import "../../common/css/index.css";
.mr10 {
  width: 195px;
}
</style>
